<?php

namespace common\models\fenhuiartic;

use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use common\models\fenhuiartic\Fenhuiartic;

/**
 * FenhuiarticSearch represents the model behind the search form about `common\models\fenhuiartic\Fenhuiartic`.
 */
class FenhuiarticSearch extends Fenhuiartic
{
    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['id', 'status', 'push', 'push_status', 'created_at', 'updated_at'], 'integer'],
            [['title', 'subhead', 'intro', 'img', 'author', 'comefrom', 'link', 'content', 'seocontent', 'seokeyword', 'isrecom','fc_id','fg_id'], 'safe'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function scenarios()
    {
        // bypass scenarios() implementation in the parent class
        return Model::scenarios();
    }

    /**
     * Creates data provider instance with search query applied
     *
     * @param array $params
     *
     * @return ActiveDataProvider
     */
    public function search($params)
    {
        if(!empty($id)) {
            $query = Fenhuiartic::find()->where(['fc_id' => $id])->orderBy('id desc');
        } else {
            $query = Fenhuiartic::find([])->orderBy('id desc');
        }
        

        // add conditions that should always apply here

        $dataProvider = new ActiveDataProvider([
            'query' => $query,
            'pagination' => [
                'pageSize' => 10,
            ],
        ]);

        $this->load($params);

        if (!$this->validate()) {
            // uncomment the following line if you do not want to return any records when validation fails
            // $query->where('0=1');
            return $dataProvider;
        }

        // grid filtering conditions
        $query->andFilterWhere([
            'id' => $this->id,
            'fc_id' => $this->fc_id,
            'fg_id' => $this->fg_id,
            'status' => $this->status,
            'created_at' => $this->created_at,
            'updated_at' => $this->updated_at,
        ]);

        $query->andFilterWhere(['like', 'title', $this->title])
            ->andFilterWhere(['like', 'subhead', $this->subhead])
            ->andFilterWhere(['like', 'intro', $this->intro])
            ->andFilterWhere(['like', 'img', $this->img])
            ->andFilterWhere(['like', 'author', $this->author])
            ->andFilterWhere(['like', 'comefrom', $this->comefrom])
            ->andFilterWhere(['like', 'link', $this->link])
            ->andFilterWhere(['like', 'content', $this->content])
            ->andFilterWhere(['like', 'seocontent', $this->seocontent])
            ->andFilterWhere(['like', 'seokeyword', $this->seokeyword])
            ->andFilterWhere(['like', 'isrecom', $this->isrecom]);

        return $dataProvider;

    }
}
